import api from '@/utils/http.api.js'
import { CURRENT_USER, ACCESS_TOKEN, USER_AVATAR } from '@/store/mutation-types'
import { storage } from '@/utils/storage'
import util from '@/utils/util'
export default {
  namespaced: true,
  state: {
    userInfo: storage.get(CURRENT_USER, null), // 用户信息
    token: storage.get(ACCESS_TOKEN, null), // 登录token
    avatar: storage.get(USER_AVATAR, ''), // 用户头像
    // 用户礼包
    ticket: {
      visible: false,
      newUser: 0, // 是否是新用户
      list: [],
    },
  },
  getters: {
    getUserInfo(state) {
      let info = state.userInfo
      if (util.isDef(info)) {
        return info
      }
      return {}
    },
    getToken(state) {
      let token = state.token
      if (util.isDef(token)) {
        return token
      }
      return {
        ak: '',
        sk: '',
      }
    },
    getHasLogin(state) {
      let token = state.token
      return util.isDef(token)
    },
    getUserAvatar(state) {
      return state.avatar
    },
  },
  mutations: {
    setUserInfo(state, data) {
      state.userInfo = data
      storage.set(CURRENT_USER, data)
    },
    setToken(state, data) {
      state.token = data.auth_token
      storage.set(ACCESS_TOKEN, data.auth_token)

      // 处理新用户礼包
      if (!uni.$u.test.empty(data.ticket)) {
        // 显示用户礼包
        setTimeout(() => {
          state.ticket.visible = true
        }, 2000)

        // 用户礼包数据
        state.ticket.list = data.ticket
      }
    },
    setAvatar(state, img) {
      state.avatar = img
      storage.set(USER_AVATAR, img)
    },
    // 退出登录
    logout(state) {
      // 清除token
      state.token = null
      storage.set(ACCESS_TOKEN, null)
      // 清除用户信息
      state.userInfo = null
      storage.set(CURRENT_USER, null)
      // 清除用户头像
      state.avatar = ''
      storage.set(USER_AVATAR, '')
    },
    // 关闭用户礼包
    useUserTicketDialog(state, status) {
      state.ticket.visible = status
    },
  },
  actions: {
    /* 用户信息 */
    setUserInfo({ state, commit }, info) {
      api.getAccountInfo().then((res) => {
        const { data } = res
        let obj = {}
        if (util.isDef(state.userInfo)) {
          obj = Object.assign(obj, state.userInfo)
        }
        if (util.isDef(info)) {
          obj = Object.assign(obj, info)
        }
        obj = Object.assign(obj, data)
        commit('setUserInfo', obj)
      })
    },
    // 登出
    logout({ commit }) {
      commit('logout')
      // 清除购物车数量
      commit('projectSetting/comGlobCartNumber', 0, {
        root: true,
      })
      // 清除未读消息
      commit('projectSetting/comGlobNotifyUnread', 0, {
        root: true,
      })
      // 清空分享
      commit('projectSetting/setShareCode', '', {
        root: true,
      })
      // 清除openid
      commit('projectSetting/comGlobConfigopenid', '', {
        root: true,
      })
    },
  },
}

/** 登陆 **/
const state = {
  userInfo: null, // 用户信息
  token: null, // 登录token
}

const mutations = {
  ['TOKEN'](state, token) {
    state.hasLogin = true
    state.token = token
    uni.setStorageSync('vuex_token', JSON.stringify(token))
  },
  ['LOGOUT'](state, token) {
    state.hasLogin = false
    state.token = {}
    uni.removeStorageSync('vuex_token')
  },
  ['USERINFO'](state, data) {
    state.userInfo = {}
    uni.setStorageSync('eduline_userinfo', JSON.stringify(data))
  },
  /* 登录 */
  login(state, user) {
    state.hasLogin = true
    state.userInfo = user
  },
  /* 登出 */
  logout(state, user) {
    state.hasLogin = false
    state.token = {}
    state.userInfo = {}
    uni.removeStorageSync('eduline_userinfo')
    uni.removeStorageSync('vuex_token')
  },
  /* token */
  getToken(state, token) {
    state.token = token
  },
  /*更新用户信息*/
  getUserInfo(state, item) {
    state.userInfo = item
    state.hasLogin = true
  },
  // 更新配置信息
  updataConfig(state, item) {
    state.config = item
  },
}

const actions = {
  /**
   * @description 登录
   * @param {Object} data 用户信息
   */
  loginToken({ commit }, token) {
    commit('TOKEN', token)
  },
  /**
   * @description 注销用户清除token
   */
  logout({ commit }) {
    commit('LOGOUT')
  },
  /**
   * @description 注销用户清除token
   */
  setUserInfo({ commit }, data) {
    commit('USERINFO', data)
  },
}

const getters = {
  token(state) {
    if (uni.getStorageSync('vuex_token')) {
      return JSON.parse(uni.getStorageSync('vuex_token'))
    }
    return state.token
  },
  userinfo(state) {
    if (uni.getStorageSync('eduline_userinfo')) {
      return JSON.parse(uni.getStorageSync('eduline_userinfo'))
    }
    return state.userInfo
  },
}
